using System.Data;
using System.Runtime.CompilerServices;
using MySqlConnector;

namespace Front._1127;

public partial class Bookform : Form
{
    public Bookform()
    {
        InitializeComponent();
    }

    private void Bookform_Load(object sender, EventArgs e)
    {
        //加载所有图书信息
        GetAllBooks();

        //获取所有图书分类信息
        GetAllBookCategories();
    }

    private void GetAllBookCategories()
    {
        using var connection =
            new MySqlConnection(
                "server=localhost;port=3306;database=book;user=root;password=123456;charset=utf8mb4;AllowPublicKeyRetrieval=True;");
        connection.Open();
        //sql
        string sql = "select book_category from book_info;";
        //创建适配器对象
        MySqlDataAdapter adapter = new MySqlDataAdapter(sql, connection);
        //创建数据集
        DataSet dataSet = new DataSet();
        //填充数据
        adapter.Fill(dataSet, "book_category");
        //插入全部行
        DataTable dataTable = dataSet.Tables["book_category"];
        DataRow row = dataTable.NewRow();
        row[0] = "全部";
        dataTable.Rows.InsertAt(row, 0);
        //绑定数据
        this.comboBox1.DataSource = dataTable;
        this.comboBox1.DisplayMember = dataTable.Columns[0].ColumnName;
    }

    private void GetAllBooks()
    {
        using var connection =
            new MySqlConnection(
                "server=localhost;port=3306;database=book;user=root;password=123456;charset=utf8mb4;AllowPublicKeyRetrieval=True;");
        connection.Open();
        //sql
        string sql = "SELECT book_id,isbn,book_name,author FROM book_info;";
        //创建适配器对象
        MySqlDataAdapter adapter = new MySqlDataAdapter(sql, connection);
        //创建数据集
        DataSet dataSet = new DataSet();
        //填充数据
        adapter.Fill(dataSet, "book_info");
        //数据绑定
        this.dataGridView1.DataSource = dataSet.Tables["book_info"];
    }

    private void button2_Click(object sender, EventArgs e)
    {
        AddBookform addBookForm = new AddBookform();
        addBookForm.Show();
    }
}
    
    
